<template>
  <basic-container>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>
      <avue-crud :data="levyResultData"
                 :option="tableoption"
                 :page="page"
                 :table-loading="tableLoading"
                 @size-change="sizeChange"
                 @current-change="currentChange"
                 @search-change="searchChange"
                 @search-reset="searchReset"
                 class="avuecrud">
        <template slot-scope="scope" slot="menu">
          <div class="menu-title" @click.stop="collectionFeedback(scope.row,scope.index)"
               v-if="!(scope.row.state == '0' || scope.row.state == '1' || scope.row.state == '2')">
            <i class="iconfont iconview icon-primary"></i>
            <p class="menu-title-text">查看反馈</p>
          </div>
        </template>
      </avue-crud>
      <el-dialog title="反馈" :visible.sync="dialogVisible" width="70%">
        <el-form
          label-width="130px"
          :model="projectForm"
          style="padding-right: 30px;"
          ref="projectForm"
        >
          <el-row>
            <el-col :span="11">
              <el-form-item class="form-item-release" label="意见类型 :" prop="hasOpinion">
                <!-- <el-radio v-model="projectForm.hasOpinion" label="0">无意见</el-radio> -->
                <el-radio v-model="projectForm.hasOpinion" label="1">衔接意见</el-radio>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="22">
              <el-form-item class="form-item-release" label="衔接意见 :" prop="opinion">
                <el-input
                  type="textarea"
                  :autosize="{ minRows: 4, maxRows: 4}"
                  placeholder="请输入衔接意见"
                  v-model="projectForm.opinion"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item class="form-item-release" label="联系人 :" prop="contactName">
                <el-input
                  v-model="projectForm.contactName"
                  placeholder="请输入联系人"
                  style="width: 280px"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item class="form-item-release" label="联系电话 :" prop="contactNumber">
                <el-input
                  v-model="projectForm.contactNumber"
                  placeholder="请输入联系电话"
                  style="width: 280px"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <div style="margin-top: 18px;margin-left: 78px;">
          <p class="" style="float:left;font-weight: 600;margin-bottom: 0px;">附件 ：</p>
          <div v-for="(file,index) in files" :key="index" class="relateddivsubject">
            <el-tooltip class="item" effect="dark" content="下载资料" placement="top-start">
              <el-button class="downbtn" @click="fileDownload(file.fileId,file.fileName)">
                <img src="img/download-blue.png" class="detail-data-img"/><span>下载</span>
              </el-button>
            </el-tooltip>
            <p class="detail-data-file" v-if="file.fileName">{{file.fileName}}</p>
          </div>
          <p class="detail-data-file" v-if="files.length==0">无</p>
        </div>

        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">关 闭</el-button>
        </span>
      </el-dialog>
    </div>
    <!--不采纳弹窗-->
    <el-dialog title="不采纳理由" :visible.sync="dialogVisibleDisagree">
      <el-input
        type="textarea"
        v-model="dialogRejectReason"
        rows="3"
      >
      </el-input>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleDisagree = false">关 闭</el-button>
      </div>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {connectionFeedback, getPlanConnectionSingle} from "@/api/make";
  import {makeConnectFormOpinion,formOpinion,rejectOpinionPage} from "@/api/opinion";
  import {getNotNull,getFileTypeFiles} from '@/views/util';
  import {getfileinfopage} from "@/api/system"
  import relatedfile from "@/mixins/relatedfile";
  import {collectionFeedbackRules} from "@/const/infoRules";
  import getDetailData from "@/mixins/getDetailData";
  import {resultDetail} from "@/api/planning/result";
  import {opinionTagertList} from "@/api/opinion"
  import {toDoTargetOpinion} from "@/api/review"
  import fileTypeMixin from "@/mixins/fileTypeMixin";


  export default {
    name: "todoConnectionfinished",
    mixins: [relatedfile, getDetailData(),fileTypeMixin],
    created() {
      this.getResultList(this.page);
      window.rejectReason = this.rejectReason;
    },
    data() {
      return {
        headerData: {
          title: '规划衔接'
        },
        levyResultData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        tableoption: {
          page: true,
          menuWidth: 120,
          addBtn: false,
          editBtn: false,
          addRowBtn: false,
          indexLabel: "序号",
          index: true,
          cellBtn: false,
          delBtn: false,
  indexWidth:70,
          columnBtn: false,
          refreshBtn: false,
          filterBtn: false,
          searchBtn: false,
          keyId: "id",
          column: [{
            label: "衔接主题",
            prop: "title",
            search: true,
            cell: true
          },
            {
              label: "发起时间",
              width: 150,
              prop: "createTime",
              cell: true,
              formatter: function (row, value, label, column) {
                return value.split(' ')[0]
              }
            },
            {
              label: "反馈部门",
              width: 150,
              prop: "targetDeptName",
              cell: true
            },
            {
              label: "状态",
              width: 80,
              prop: "state",
              cell: true,
              formatter: function (row, value, label, column) {
                switch (row.state) {
                  case '0':
                    return "<font color='red'>" + "催办" + "</font>";
                  case '1':
                    return "<font color='red'>" + "待反馈" + "</font>";
                  case '2':
                    return "<font color='#DCA450'>" + "退回" + "</font>";
                  case '3':
                    // return "<font color='#DCA450'>" + "不采纳" + "</font>";
                    return '<a style="cursor: pointer;color:#DCA450;" onclick="rejectReason(\'' + row.targetId + '\')">' + '不采纳' + '</a>';
                  case '4':
                    return "<font color='#7EBE50'>" + "已反馈" + "</font>";
                  case '5':
                    return "<font color='#7EBE50'>" + "已采纳" + "</font>";
                }
              }
            },

          ]
        },
        tableLoading: false,
        dialogVisible: false,
        detailData: [],
        fileIdstemp: [],
        projectForm: {
          opinion: '',
          hasOpinion: '1',
        },
        collectionFeedbackRules: collectionFeedbackRules,
        activeNames: ['0'],
        hasOpinionFlag: true,
        opinionRules:{
          opinion: [
            {required: true, message: "请输入衔接意见", trigger: "blur"}
          ],
          contactName: [
            {required: true, message: "请输入联系人", trigger: "blur"}
          ],
          contactNumber: [
            {required: true, message: "请输入联系电话", trigger: "blur"}
          ],
        },
        notOpinionRules:{
          opinion: [
            {required: false}
          ],
          contactName: [
            {required: true, message: "请输入联系人", trigger: "blur"}
          ],
          contactNumber: [
            {required: true, message: "请输入联系电话", trigger: "blur"}
          ],
        },
        files:[],
        dialogVisibleDisagree:false,
        dialogRejectReason:'',
        titleLikeSearch:'',
      }
    },
    watch: {
      'projectForm.hasOpinion'(val) {
        if (val == '1') {
          this.hasOpinionFlag = true
        }
        if (val == '0') {
          this.hasOpinionFlag = false
        }
      }
    },
    methods: {
      // 课题详情跳转
      collectionFeedback(row) {
        this.projectForm.targetId = row.targetId;
          //查看当前部门自己反馈的表单
          formOpinion(Object.assign({
            opinionId:row.opinionId,
            targetId:row.targetId,
          })).then(async response =>{
            var records = response.data.data.records[0];
            this.projectForm.contactName = records.contactName;
            this.projectForm.contactNumber = records.contactNumber;
            this.projectForm.opinion = records.opinion;
            if(records.fileIds!=null){
              if(records.fileIds.length!=0){
                this.files = await getFileTypeFiles(records.fileIds);
              }
            }
            if(records.hasOpinion!=null){
              if(records.hasOpinion){
                 this.projectForm.hasOpinion = '1'
              }else {
                this.projectForm.hasOpinion = '0'
              }
            }
          });

        this.dialogVisible = true;
      },

      currentChange(currentPage) {
        var page = this.page;
        page.currentPage = currentPage;
        let params = {
          titleLike:this.titleLikeSearch,
        };
        this.getResultList(page,params);
      },
      sizeChange(pageSize) {
        var page = this.page;
        page.pageSize = pageSize;
        let params = {
          titleLike:this.titleLikeSearch,
        };
        this.getResultList(page,params);
      },

      getResultList(page, params) {
        toDoTargetOpinion(Object.assign({
          current: page.currentPage,
          size: page.pageSize,
          type: '6',
          states:'3,4,5'
        },params)).then(response => {
          this.levyResultData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.$emit("getTotal", response.data.data.total)
        })
      },
      /**
       * 刷新回调
       */
      searchReset() {
        this.titleLikeSearch = '';
        let params = {
          titleLike:this.titleLikeSearch,
        };
        this.getResultList(this.page,params);
      },
      //搜索回调
      searchChange (params) {
        this.page.currentPage = 1
        this.getResultList(this.page, this.dealSearchParam(params));
      },
      // 处理搜索过滤
      dealSearchParam(params) {
        if (params.title) {
          params.titleLike = params.title;
        }
        this.titleLikeSearch = params.title;
        return params;
      },
      commitYes() {
        this.$refs.projectForm.validate(valid => {
          if (valid) {
            this.$confirm("确定提交吗?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              //将多个相关材料的附件id拼成逗号隔开的字符串放在 this.projectForm.fileId 中
              var arr = [];
              for (var item in this.fileListMixin) {
                arr.push(this.fileListMixin[item].url);
              }
              this.projectForm.fileIds = arr;
              this.projectForm.hasOpinion = parseInt(this.projectForm.hasOpinion);
              makeConnectFormOpinion(this.projectForm).then(response => {
                if (response.data.code == 0) {
                  this.$notify({
                    title: "成功",
                    message: "发布成功",
                    type: "success",
                    duration: 2000
                  });
                  this.$router.go(0);
                }
              })
            })
          }
        })

      },
      //不采纳理由弹窗
      rejectReason(targetId){
        rejectOpinionPage(Object.assign({
          targetId:targetId,
        })).then(response =>{
          if (response.data.code == '0') {
            if(response.data.data.records!=null&&response.data.data.records.length!=0){
              this.dialogRejectReason = response.data.data.records[0].opinion
            }else {
              this.dialogRejectReason = '无'
            }
            this.dialogVisibleDisagree = true;
          }
        })
      },
    }
  }
</script>

<style lang="scss" scoped>

</style>
